package org.hydrakyoufeng.dm.toolkit;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hydrakyoufeng.j2ee.HKFController;
import org.hydrakyoufeng.j2ee.ParamResultData;
import org.hydrakyoufeng.j2ee.RequestHandler;
import org.hydrakyoufeng.lang.HKFKeys;
import org.hydrakyoufeng.lang.HKFValues;
import org.hydrakyoufeng.tool.StringTool;
import org.hydrakyoufeng.tool.collection.MapTool;
import org.hydrakyoufeng.tool.collection.MapUtil;
import org.hydrakyoufeng.tool.json.JsonSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;

public abstract class AbstractController implements HKFController {

	protected Object service;
	
	protected RequestHandler requestHandler;
		
	@Autowired
	private LogThread logThread;
	
	@Autowired
	private JsonSerializer JsonSerializer;
	
	@Autowired
	@Qualifier("requestHandler")
	public void setRequestHandler(RequestHandler requestHandler) {
		this.requestHandler = requestHandler;
	}
	
	@Override
	public Object getService() {
		return service;
	}

	@Override
	public RequestHandler getRequestHandler() {
		return requestHandler;
	}
	
	/**
	 * 通用入口
	 * @param request
	 * @param res
	 */
	@RequestMapping("{method}")
	public void startAction(HttpServletRequest request,
			HttpServletResponse res) {
		try {
			res.setContentType("application/json; charset=utf-8");
			doAction(request, res);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
	
	/**
	 * 异常处理
	 */
	@Override
	public boolean invokeException(ServletRequest request, 
			ServletResponse response, 
			Exception e,
			ParamResultData paramResultData) throws ServletException {
		String msg = StringTool.getFirst(e.getMessage(),"服务器发生异常");
		response.setContentType("application/json");
		String str = "{\"status\":" + HKFValues.STATUS_EXCEPTION + ",\"msg\":" + JsonSerializer.getString(msg) + "}";
		try {
			response.getOutputStream().write(str.getBytes("UTF-8"));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 重写方法，用于记录日志
	 */
	@Override
	public boolean invokeResult(HttpServletRequest request, 
			ServletResponse res, 
			Object result,
			ParamResultData paramResultData) throws Exception {
		Map<String,Object> pm = MapUtil.popHashMap();
		Map<String,Object> map = paramResultData.getResultData();
		pm.put("authorition", MapTool.getString(map,HKFKeys.HEADER_SCOPE + ".Authorition"));
		pm.put("ip", MapTool.getString(map, HKFKeys.REQUEST_PROPERTIES + ".remoteAddr"));
		pm.put("uri", request.getRequestURI());
		pm.put("request", JsonSerializer.getStringNotExists(map,"loginAdmin","loginUser"));
		pm.put("user_pk", MapTool.getString(map, "loginUser.t_user_pk"));
		pm.put("admin_pk", MapTool.getString(map, "loginAdmin.t_admin_pk"));
		pm.put("response", JsonSerializer.getString(result));
		logThread.offer(pm);
		return true;
	}
	
	/**
	 * 交由统一的日志线程输出
	 */
	@Override
	public boolean isLogParameter(HttpServletRequest request) {
		return false;
	}
	
	/**
	 * 交由统一的日志线程输出
	 */
	@Override
	public boolean isLogResult(HttpServletRequest request) {
		return false;
	}
	
}
